﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;


namespace ex_uppgift
{
    public partial class Form1 : Form
    {

        private DateTime crmTime = new DateTime();
 

        public static List<Contact> contacts = new List<Contact>();
        public static List<Relative> relatives = new List<Relative>();

        ContactManagement contactManagementClass = new ContactManagement();
        Calendar calendarClass = new Calendar();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Serializer.DeserializeContacts();
            lbxContactList.DataSource = contacts;
            lbxSelectContactToMeeting.DataSource = Form1.contacts;
            grpbxAddRelative.Visible = false;
            grpbxNeewMeeting.Visible = false;

            cbxTypeOfRelative.DataSource = Enum.GetValues(typeof(Relative.FamilyMember));
        }

        public void RemoveButtonVisibiltity()
        {
            if (lbxContactList.Items.Count == 0)
            {
                btnContactRemove.Enabled = false;
                lbxContactList.DataSource = null;
                lbxContactList.DataSource = contacts;
            }

            else
            {
                btnContactRemove.Enabled = true;
                lbxContactList.DataSource = null;
                lbxContactList.DataSource = contacts;
            }
        }

        private void btnAddContact_Click(object sender, EventArgs e)
        {
            if (CheckFieldsInAddContact() == false)
            {
                MessageBox.Show("You have to fill all fields");
            }

            else
            {
                contactManagementClass.CreateContact();
            }
        }

        private bool CheckFieldsInAddContact()
        {
            if (tbxAddName.Text == String.Empty ||
                            tbxAddEmailaddress.Text == String.Empty ||
                            tbxAddCompany.Text == String.Empty ||
                            tbxAddId.Text == String.Empty ||
                            tbxAddPhoneNumber.Text == String.Empty ||
                            cbxAddBirthdayMonth.SelectedItem == null ||
                            cbxAddBirthdayDay.SelectedItem == null ||
                            tbxAddHobbies.Text == String.Empty ||
                            tbxAddCity.Text == String.Empty ||
                            tbxAddZipcode.Text == String.Empty ||
                            tbxAddCity.Text == String.Empty)
            {

                return false;

            }
            else
            {
                return true;
            }
        }

        public void EmptyAddContactFields()
        {
            foreach (Control ctrl in grpbxAddContact.Controls)
            {
                if (ctrl is TextBox)
                {
                    (ctrl as TextBox).Clear();
                }
                else
                    if (ctrl is ComboBox)
                    {
                        (ctrl as ComboBox).Text = String.Empty;
                    }
            }
        }


        private void btnAddChangePicture_Click(object sender, EventArgs e)
        {
            openFDChangePicture.ShowDialog();
            openFDChangePicture.Title = " Select picture";
            openFDChangePicture.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFDChangePicture.Filter = "JPEG|*.jpg";
            string ChosenPicture = "";
            ChosenPicture = openFDChangePicture.FileName;
            picbxContact.Image = Image.FromFile(ChosenPicture);


            if (openFDChangePicture.ShowDialog() == DialogResult.Cancel)
            {
                MessageBox.Show("You didn't choose a picture");
            }
            else
            {
                picbxContact.BackgroundImage = null;
                ChosenPicture = openFDChangePicture.FileName;
                picbxContact.Image = Image.FromFile(ChosenPicture);
            }
        }

        private void lbxContactList_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbxRelatives.Items.Clear();
            Serializer.SerializeRelative();

            var selectedContactInformation = contacts
            .Where(contact => contact.PersonId == (lbxContactList.SelectedItem as Contact).PersonId)
            .Select(contact => new
            {
                contact.Name,
                contact.PhoneNumber,
                contact.Street,
                contact.ZipCode,
                contact.City,
                contact.Hobbies,
                contact.MonthBirthday,
                DayBithday = contact.DayBirthday
            }).ToList();

            var selectedContactRelatives = relatives
            .Where(relative => relative.WhosRelative == (lbxContactList.SelectedItem as Contact).PersonId)
            .Select(relative => new
            {
                relative.Name,
                relative.PersonId,
                relative.MonthBirthday,
                relative.DayBirthday
            }).ToList();

            foreach (var relative in selectedContactRelatives)
            {
                lbxRelatives.Items.Add(relative);

            }
            gridviewContactInformation.DataSource = selectedContactInformation;

        }


        private void btnContactRemove_Click(object sender, EventArgs e)
        {
            contactManagementClass.RemoveContact();
        }


        private void btnContactAddRelative_Click(object sender, EventArgs e)
        {
            grpbxAddRelative.Visible = true;
           
        }


        private void btnAddRelative_Click(object sender, EventArgs e)
        {
            contactManagementClass.AddRelative();
        }

        private void btnRemoveRelative_Click(object sender, EventArgs e)
        {
            
        }


        private void mCalCalendar_DateChanged(object sender, DateRangeEventArgs e)
        {
        
        }


        private void btnBookNewMeeting_Click(object sender, EventArgs e)
        {
            grpbxNeewMeeting.Visible = true;

        }

        private void lbxSelectContactToMeeting_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void btnAddSelectedContactToMeeting_Click(object sender, EventArgs e)
        {

        }


    }
}

